package com.vehicle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vehicle.bean.Admin;
import com.vehicle.exception.VehicleException;
import com.vehicle.feign.ContactFeign;
import com.vehicle.feign.MenuFeign;
import com.vehicle.mapper.AdminMapper;
import com.vehicle.result.ResponseEnum;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vehicle.service.AdminService;
import com.vehicle.util.RegexValidateUtil;
import com.vehicle.vo.AdminListVO;
import com.vehicle.vo.AdminLoginVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 校园车辆管理系统
 * @since 2023-04-17
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    ContactFeign contactFeign;

    @Autowired
    MenuFeign menuFeign;

    @Override
    public Admin getAdminByTel(String tel) {
        //验证手机号号
//        boolean b1 = RegexValidateUtil.checkMobile(tel);
//        if(!b1){
//            throw new VehicleException(ResponseEnum.MOBILE_ERROR.getMsg());
//        }
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<Admin>();
        adminQueryWrapper.eq("tel",tel);
        Admin admin = adminMapper.selectOne(adminQueryWrapper);
        System.out.println(admin);
        if(admin == null){
            throw new VehicleException(ResponseEnum.MOBILE_IS_NULL.getMsg());
        }
        return admin;
    }

    @Override
    public Admin getAdminByName(String name) {
        //查询用户
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<Admin>();
        adminQueryWrapper.eq("admin_name",name);
        Admin admin = adminMapper.selectOne(adminQueryWrapper);
        //检验用户
        if(admin == null){
            throw new VehicleException(ResponseEnum.USERNAME_ERROR.getMsg());
        }
        return admin;
    }

    @Override
    public Admin getAdminByTelAndPassword(String tel, String password) {
        //验证手机号号
        boolean b1 = RegexValidateUtil.checkMobile(tel);
        if(!b1){
            throw new VehicleException(ResponseEnum.MOBILE_ERROR.getMsg());
        }
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<Admin>();
        adminQueryWrapper.eq("tel",tel);
        adminQueryWrapper.eq("password",password);
        Admin admin = adminMapper.selectOne(adminQueryWrapper);
        if(admin == null){
            throw new VehicleException(ResponseEnum.MOBILE_IS_NULL.getMsg());
        }
        return admin;
    }

    @Override
    public boolean updatePasswordOrTokenById(Integer id, String password,String token) {
        UpdateWrapper<Admin> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("admin_id",id);
        if(password!=null){
            updateWrapper.set("password",password);
        }
        if(token!=null){
            updateWrapper.set("token",token);
        }
        int b = adminMapper.update(null,updateWrapper);
        if(b==1){
            return true;
        }
        return false;
    }

    @Override
    public AdminListVO adminList(Integer page,Integer size) {
        Page<Admin> infoPage = new Page<>(page,size);
        Page<Admin> adminInfoPage = adminMapper.selectPage(infoPage,null);
        AdminListVO adminListVO = new AdminListVO();
        adminListVO.setSize(adminInfoPage.getSize());
        adminListVO.setTotal(adminInfoPage.getTotal());
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        List<AdminLoginVO> adminLoginVOS = new ArrayList<>();
        List<Admin> adminList = adminInfoPage.getRecords();
        for (Admin admin:adminList) {
            AdminLoginVO adminLoginVO = new AdminLoginVO();
            BeanUtils.copyProperties(admin,adminLoginVO);
            //
            int collegeId = contactFeign.getCollageIdById(admin.getCdId());
            adminLoginVO.setCollegeId(collegeId);
            adminLoginVO.setDetail(contactFeign.getDetailById(admin.getCdId()));
            adminLoginVO.setCollegeName(contactFeign.getCollageNameById(collegeId));
            int roleId = menuFeign.getRoleIdByAdmin(admin.getAdminId());
            adminLoginVO.setRoleId(roleId);
            adminLoginVO.setRoleName(menuFeign.getRoleNameById(roleId));
            int uniId = contactFeign.getUniverIdById(collegeId);
            adminLoginVO.setUniverId(uniId);
            adminLoginVO.setUniverName(contactFeign.getNameUniversity(uniId));
            adminLoginVOS.add(adminLoginVO);
        }
       adminListVO.setContent(adminLoginVOS);
       return adminListVO;
    }

    @Override
    public Admin addAdminMessage(Admin admin) {
        int b = adminMapper.insert(admin);
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("admin_id",admin.getAdminId());
        Admin admin1 = adminMapper.selectOne(adminQueryWrapper);
        return admin1;
    }

    @Override
    public boolean adminNameExist(String adminName) {
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<Admin>();
        adminQueryWrapper.eq("admin_name",adminName);
        Admin admin = adminMapper.selectOne(adminQueryWrapper);
        if(admin != null){
            return false;
        }
        return true;
    }

    @Override
    public boolean adminTelExist(String tel) {
        QueryWrapper<Admin> adminQueryTel = new QueryWrapper<Admin>();
        adminQueryTel.eq("tel",tel);
        Admin admin = adminMapper.selectOne(adminQueryTel);
        if(admin != null){
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteAdminById(Integer id) {
        int b = adminMapper.deleteById(id);
        if(b==1){
            return true;
        }
        return false;
    }

    @Override
    public AdminLoginVO getLoginMessage(Admin admin) {
        AdminLoginVO adminLoginVO = new AdminLoginVO();
        BeanUtils.copyProperties(admin,adminLoginVO);
        //
        int collegeId = contactFeign.getCollageIdById(admin.getCdId());
        adminLoginVO.setCollegeId(collegeId);
        adminLoginVO.setDetail(contactFeign.getDetailById(admin.getCdId()));
        adminLoginVO.setCollegeName(contactFeign.getCollageNameById(collegeId));
        int uniId = contactFeign.getUniverIdById(collegeId);
        int roleId = menuFeign.getRoleIdByAdmin(admin.getAdminId());
        adminLoginVO.setRoleName(menuFeign.getRoleNameById(roleId));
        adminLoginVO.setRoleId(roleId);
        adminLoginVO.setUniverId(uniId);
        adminLoginVO.setUniverName(contactFeign.getNameUniversity(uniId));
        return adminLoginVO;
    }

    @Override
    public List<AdminLoginVO> getListByContent(String content) {
        QueryWrapper<Admin> adminQueryWrapper1 = new QueryWrapper<>();
        adminQueryWrapper1.like("admin_name",content);
        List<Admin> adminList1 = adminMapper.selectList(adminQueryWrapper1);
        QueryWrapper<Admin> adminQueryWrapper2 = new QueryWrapper<>();
        adminQueryWrapper2.like("tel",content);
        List<Admin> adminList2 = adminMapper.selectList(adminQueryWrapper2);
        for (Admin a: adminList2) {
            if(!adminList1.contains(a)){
                adminList1.add(a);
            }
        }
        List<AdminLoginVO> adminLoginVOS = new ArrayList<>();
        for (Admin admin:adminList1) {
            AdminLoginVO adminLoginVO = new AdminLoginVO();
            BeanUtils.copyProperties(admin,adminLoginVO);
            //
            int collegeId = contactFeign.getCollageIdById(admin.getCdId());
            adminLoginVO.setCollegeId(collegeId);
            adminLoginVO.setDetail(contactFeign.getDetailById(admin.getCdId()));
            adminLoginVO.setCollegeName(contactFeign.getCollageNameById(collegeId));
            int roleId = menuFeign.getRoleIdByAdmin(admin.getAdminId());
            adminLoginVO.setRoleId(roleId);
            adminLoginVO.setRoleName(menuFeign.getRoleNameById(roleId));
            int uniId = contactFeign.getUniverIdById(collegeId);
            adminLoginVO.setUniverId(uniId);
            adminLoginVO.setUniverName(contactFeign.getNameUniversity(uniId));
            adminLoginVOS.add(adminLoginVO);
        }

        return adminLoginVOS;
    }
}
